package com.example.newappchildrchinese;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activeandroid.query.Select;
import com.example.newappchildrchinese.base.BaseActivity;
import com.example.newappchildrchinese.mode.ChineseClass;
import com.example.newappchildrchinese.mode.ChineseCourse;
import com.example.newappchildrchinese.widget.WidgetCourse;

import java.util.List;

public class ClassActivity extends BaseActivity implements View.OnClickListener {

    private ImageView topbar_ico;
    private TextView topbar_title,btn_study,btn_work,btn_test,btn_number;
    private LinearLayout course_box;
    private int class_id;
    private List<ChineseCourse> chineseCoursesList;
    private Long selectCourseId;
    private ChineseClass chineseClass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class);

        findView();

        showData();
    }

    private void showData() {

        class_id = getIntent().getIntExtra("class_id", 0);

        chineseClass = new Select().from(ChineseClass.class).where("class_id=?", class_id).executeSingle();
        if (chineseClass != null) {
            topbar_title.setText(chineseClass.getClass_title());
        }

        chineseCoursesList = new Select().from(ChineseCourse.class).where("class_id=?", class_id).execute();
        for (ChineseCourse chineseCourse : chineseCoursesList) {
            WidgetCourse widgetCourse = new WidgetCourse(this);
            widgetCourse.setTitle(chineseCourse.getCourse_title() + "");
            widgetCourse.setCourseId(chineseCourse.getId());
            course_box.addView(widgetCourse);
            widgetCourse.setOnClickListener(this);
        }

        WidgetCourse widgetCourse = (WidgetCourse) course_box.getChildAt(0);
        if (widgetCourse != null) {
            widgetCourse.isSel(true);
            selectCourseId = widgetCourse.getCourseId();
        }
    }

    private void findView() {
        topbar_ico = (ImageView) findViewById(R.id.topbar_ico);
        topbar_ico.setOnClickListener(this);
        topbar_title = (TextView) findViewById(R.id.topbar_title);

        btn_study = (TextView) findViewById(R.id.btn_study);
        btn_work = (TextView) findViewById(R.id.btn_work);
        btn_test = (TextView) findViewById(R.id.btn_test);
        btn_number = (TextView) findViewById(R.id.btn_number);

        course_box = (LinearLayout) findViewById(R.id.course_box);

        btn_study.setOnClickListener(this);
        btn_work.setOnClickListener(this);
        btn_test.setOnClickListener(this);
        btn_number.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v instanceof WidgetCourse) {
            WidgetCourse widgetCourse = (WidgetCourse) v;
            for (int i = 0; i < course_box.getChildCount(); i++) {
                WidgetCourse widgetCourse2 = (WidgetCourse) course_box.getChildAt(i);
                widgetCourse2.isSel(false);
            }
            widgetCourse.isSel(true);
            selectCourseId = widgetCourse.getCourseId();
        }
        switch (v.getId()) {
            case R.id.btn_study:
                //学习
                Intent intent = new Intent(this,StudyActivity.class);
                intent.putExtra("course_id",selectCourseId);
                startActivity(intent);
                break;
            case R.id.btn_work:
                break;
            case R.id.btn_test:
                break;
            //
            case R.id.btn_number:
                break;
        }
    }
}
